//
//  BaseViewController.m
//  zoneSun
//
//  Created by Zeaple on 16/3/9.
//  Copyright © 2016年 Zeaple. All rights reserved.
//

#import "BaseViewController.h"
#import "MMDrawerController.h"
#import "CYLTabBarController.h"
@interface BaseViewController ()<UIGestureRecognizerDelegate>
//@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong)UIView *rightView;
@property(nonatomic,strong)UILabel *messageCountLabel;

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:242./255 green:242./255 blue:242./255 alpha:1.];
    //    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.navigationController.navigationBar removeFromSuperview];
    [self.view addSubview:self.naviBar];
    //返回按钮
    [self _initBackButton];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(message:) name:@"messageCount" object:nil];
   }

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    if([AppDelegate appDealegate].messageCount>0){
        self.messageCount = [AppDelegate appDealegate].messageCount;
    }
}

-(void)message:(NSNotification*)noti{
    NSInteger messageCount = [noti.object integerValue];
    self.messageCount = messageCount;
}

-(UIView *)naviBar{
    if(_naviBar==nil){
        _naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, kNaviBarHeight)];
        _naviBar.backgroundColor = [UIColor blackColor];
    }
    return _naviBar;
}

-(UILabel *)titlelabel{
    if(_titlelabel==nil){
        _titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width-130, 21)];
        _titlelabel.centerX= Main_Screen_Width/2;
        _titlelabel.centerY = self.naviBar.centerY+8;
        //        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titlelabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:17];
        _titlelabel.textColor= DEF_COLOR;
        _titlelabel.textAlignment = NSTextAlignmentCenter;
        [self.naviBar addSubview:_titlelabel];
    }
    return _titlelabel;
}

-(UIView *)rightView{
    if(_rightView==nil){
        _rightView = [[UIView alloc]initWithFrame:CGRectMake(Main_Screen_Width-15-50, 10, 50, 36)];
        [self.naviBar addSubview:_rightView];
    }
    return _rightView;
}

-(void)setVcName:(NSString *)vcName{
    _vcName = vcName;
    self.titlelabel.text = _vcName;
}
-(void)setRightItem:(UIView *)rightItem{
    _rightItem = rightItem;
    CGFloat with = _rightItem.width;
    _rightItem.left =Main_Screen_Width-15-with;
    _rightItem.centerY = 39;
    [self.naviBar addSubview:_rightItem];
}

-(void)setRightItem2:(UIView *)rightItem2{
    _rightItem2 = rightItem2;
    CGFloat with = _rightItem2.width;
    _rightItem2.left =Main_Screen_Width-15-with-5-_rightItem.width;
    _rightItem2.centerY = 39;
    [self.naviBar addSubview:_rightItem2];
}

- (void)_initBackButton {
    //取得子控制器的个数
    NSInteger count =  self.navigationController.viewControllers.count;
    if ((count > 1&&!self.isNotNeedBackBtn) || _isModel == YES) {
        if(_isModel==YES){
            [AppDelegate appDealegate].modelCount++;
            ISNOTNEEDMESSAGEBTN = YES;
        }
        UIImage *image = SetImage(@"nav_icon_back_nor");
        //        CGRectMake(15, 21.5, 25, 32)
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(4, 19, 40, 40)];
//        btn.backgroundColor = [UIColor whiteColor];
        //        btn.centerY = 37;
        [btn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [btn setImage:image forState:UIControlStateNormal];
        [self.naviBar addSubview:btn];
        //设置返回手势
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

- (void)buttonAction {
    if (_isModel) {
        [self dismissViewControllerAnimated:YES completion:nil];
        if( [AppDelegate appDealegate].modelCount==1){
              ISNOTNEEDMESSAGEBTN = NO;
                [AppDelegate appDealegate].modelCount=0;
        }else{
             [AppDelegate appDealegate].modelCount--;
        }
    }else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (UIImageView *)maskV{
    if(_maskV==nil){
        _maskV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
        _maskV.image = [UIImage imageNamed:@"wode_zhezhao"];
    }
    return _maskV;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}


-(void)setIsNeedMessageItem:(BOOL)isNeedMessageItem{
    _isNeedMessageItem = isNeedMessageItem;
    if(_isNeedMessageItem){
        [self initRightBarItem];
    }
}
-(void)setMessageCount:(NSInteger)messageCount{
    _messageCount = messageCount;
    if(_messageCount>0){
        self.messageCountLabel.hidden = NO;
        self.messageCountLabel.text = [NSString stringWithFormat:@"%ld",_messageCount];
    }else{
        self.messageCountLabel.hidden = YES;
    }
}

- (void)initRightBarItem{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame=CGRectMake(0, 0, 40, 40);
    [rightButton setImage:[UIImage imageNamed:@"nav_xinxi_nor"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    [rightButton addSubview:self.messageCountLabel];
//    self.messageCountLabel.text = @"1";
    self.messageCountLabel.hidden = YES;
    self.rightItem =rightButton;
}

-(void)rightAction{
      if([[UIApplication sharedApplication].keyWindow.rootViewController isKindOfClass:[MMDrawerController class]]){
        MMDrawerController *mm =(MMDrawerController*)[UIApplication sharedApplication].keyWindow.rootViewController;
        CYLTabBarController *tabBar = (CYLTabBarController*)mm.centerViewController;
        [tabBar setSelectedIndex:2];
    }
}

-(UILabel *)messageCountLabel{
    if(_messageCountLabel==nil){
        _messageCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(23, 4, 13, 13)];
        _messageCountLabel.font = [UIFont systemFontOfSize:9];
        _messageCountLabel.textAlignment = NSTextAlignmentCenter;
        _messageCountLabel.backgroundColor = [UIColor colorWithRed:0./255 green:216./255 blue:201./255 alpha:1.];
        _messageCountLabel.textColor =[UIColor whiteColor];
        _messageCountLabel.layer.cornerRadius = 6.5;
        _messageCountLabel.layer.masksToBounds = YES;
    }
    return  _messageCountLabel;
}

-(UIImageView *)noDataIUmageV{
    if(_noDataIUmageV==nil){
        _noDataIUmageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_zanwushuju"]];
        _noDataIUmageV.frame = CGRectMake((Main_Screen_Width-100)/2, (self.view.size.height-115)/2, 100, 115);
    }
    return _noDataIUmageV;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
